package com.ys.comapable;

import java.util.Objects;

/**
 * 描述: 学生类
 *
 * @author CodeYang
 * @version 2020/12/18 15:13
 */
public class Student implements Comparable_diy {

    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    /**
     * 比较器--规则:
     * 根据 学生的年龄进行排序-升序
     *  this.age - student.age;
     *            >  返回 正数
     *            <  返回 负数
     *            =  返回 0
     * @param arr
     * @return
     */
    @Override
    public int compareTo(Object obj) {
        // 1 判断 向下转型 使用本类数据  子类引用指向父类对象。
        if (obj instanceof Student) {
            Student student = (Student) obj;
            return this.age - student.age;
        }
        throw new IllegalArgumentException("参数不合法");
    }
}
